<template>
  <div id="login" class="login-container">
    <div class="login-box">
      <div class="avtar-box">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form label-width="0px" :rules="rules" ref="loginFormRef" :model="form" class="form-box">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn-box">
          <el-button type="primary" @click="onSubmit">登陆</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <div class='input-box'>
        <el-input placeholder='请输入内容' prefix-icon='el-icon-user-solid' v-model='input2'></el-input>
        <el-input placeholder='请输入内容' prefix-icon='el-icon-user-solid' v-model='input2'></el-input>
      </div>-->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.form)
        console.log(res.meta.status)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    onReset() {
      this.$refs.loginFormRef.resetFields()
      // console.log()
      // this.form.name = this.form.password = ''
    }
  }
}
</script>

<style lang='less' scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
}

.login-box {
  height: 300px;
  width: 450px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avtar-box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      background-color: #eee;
      border-radius: 50%;
    }
  }
  .form-box {
    position: absolute;
    width: 100%;
    bottom: 0;
    padding: 0 20px;
    box-sizing: border-box;
    .btn-box {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
